unit DatiGeneraliForm;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs,
  FMX.StdCtrls, FMX.TabControl, FMX.TMSBaseControl, FMX.TMSGridCell, FMX.TMSGridOptions, FMX.TMSGridData, FMX.TMSCustomGrid, FMX.TMSGrid, FMX.Layouts,
  Data.Bind.Components, Data.Bind.ObjectScope, BusinessObjectsU, System.Generics.Collections, Data.Bind.GenData, Fmx.Bind.GenData, Data.Bind.EngExt,
  Fmx.Bind.DBEngExt, FMX.TMSGridDataBinding, System.Rtti, System.Bindings.Outputs, Fmx.Bind.Editors, Data.Bind.Grid;

type
  TfrmDatiGenerali = class(TForm)
    TabControl1: TTabControl;
    tabAlunni: TTabItem;
    tabInsoluti: TTabItem;
    Layout1: TLayout;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    Button10: TButton;
    Button11: TButton;
    Button12: TButton;
    Button13: TButton;
    Button14: TButton;
    Button15: TButton;
    Button16: TButton;
    Button17: TButton;
    Button18: TButton;
    Button19: TButton;
    Button20: TButton;
    Button21: TButton;
    Button22: TButton;
    Button23: TButton;
    Button24: TButton;
    Button25: TButton;
    Button26: TButton;
    pbsAlunni: TPrototypeBindSource;
    BindingsList1: TBindingsList;
    GridAlunni: TTMSFMXGrid;
    LinkGridToDataSourcepbsAlunni: TLinkGridToDataSource;
    procedure BottoneRubricaClick(Sender: TObject);
    procedure pbsAlunniCreateAdapter(Sender: TObject; var ABindSourceAdapter: TBindSourceAdapter);
    procedure FormCreate(Sender: TObject);
    procedure GridAlunniFilterSelect(Sender: TObject; Col: Integer; var Condition: string);
    procedure GridAlunniGetCellLayout(Sender: TObject; ACol, ARow: Integer; ALayout: TTMSFMXGridCellLayout; ACellState: TCellState);
  private
    procedure FiltraAziendaPerIniziale(AFiltro: String);
    procedure SetColonneGridAlunni;
  public
    procedure MostraAlunni;
  end;

var
  frmDatiGenerali: TfrmDatiGenerali;

implementation

uses
  ConnectionU;

{$R *.fmx}

procedure TfrmDatiGenerali.BottoneRubricaClick(Sender: TObject);
begin
  if TButton(Sender).IsPressed then
  begin
    MostraAlunni;
    TButton(Sender).IsPressed := FALSE;
    GridAlunni.SetFocus;
  end
  else begin
    FiltraAziendaPerIniziale(TButton(Sender).Text);
    TButton(Sender).IsPressed := TRUE;
  end;
end;

procedure TfrmDatiGenerali.FiltraAziendaPerIniziale(AFiltro: String);
begin
  pbsAlunni.Active := FALSE;
  (pbsAlunni.InternalAdapter as TListBindSourceAdapter<TAlunnoView>).setList(TGestioniFactory.GestioneAlunni.GetAlunniByIniziale(AFiltro));
  pbsAlunni.Active := True;
  SetColonneGridAlunni;
end;

procedure TfrmDatiGenerali.FormCreate(Sender: TObject);
begin
  MostraAlunni;
end;

procedure TfrmDatiGenerali.GridAlunniFilterSelect(Sender: TObject; Col: Integer; var Condition: string);
begin
  if Condition = '(Tutti)' then
  begin
    Condition := '*';
  end;
end;

procedure TfrmDatiGenerali.GridAlunniGetCellLayout(Sender: TObject; ACol, ARow: Integer; ALayout: TTMSFMXGridCellLayout; ACellState: TCellState);
begin
  if (ARow > 0) then
  begin
    ALayout.Fill.Color := TAlphaColorRec.White;
    ALayout.FontFill.Color := TAlphaColorRec.Black;
    if not TTMSFMXGrid(Sender).Cells[TTMSFMXGrid(Sender).Columns.ColumnByID('cmAttivo').Index, ARow].ToBoolean then
    begin
      ALayout.Fill.Color := TAlphaColorRec.Red;
      ALayout.FontFill.Color := TAlphaColorRec.Yellow;
    end
  end;
end;

procedure TfrmDatiGenerali.MostraAlunni;
begin
  pbsAlunni.Active := FALSE;
  (pbsAlunni.InternalAdapter as TListBindSourceAdapter<TAlunnoView>).setList(TGestioniFactory.GestioneAlunni.GetAlunni);
  pbsAlunni.Active := True;
  SetColonneGridAlunni;
end;

procedure TfrmDatiGenerali.pbsAlunniCreateAdapter(Sender: TObject; var ABindSourceAdapter: TBindSourceAdapter);
begin
  ABindSourceAdapter := TListBindSourceAdapter<TAlunnoView>.create(pbsAlunni, TObjectList<TAlunnoView>.create);
end;

procedure TfrmDatiGenerali.SetColonneGridAlunni;
begin
  GridAlunni.ColumnWidths[0] := 20;
  GridAlunni.ColumnWidths[1] := 200;
  GridAlunni.ColumnWidths[2] := 100;
  GridAlunni.ColumnWidths[3] := 100;
  GridAlunni.ColumnWidths[4] := 100;
  GridAlunni.ColumnWidths[5] := 50;
  GridAlunni.ColumnWidths[6] := 0;
  GridAlunni.Cells[1, 0] := 'Nome';
  GridAlunni.Cells[2, 0] := 'Sezione';
  GridAlunni.Cells[3, 0] := 'Inquadramento';
  GridAlunni.Cells[4, 0] := 'Telefono';
  GridAlunni.Cells[5, 0] := 'Attivo';

end;

end.
